home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 October: Mac OS SDK / Dev.CD Oct 97 SDK1.toast / Development Kits (Disc 1) / Apple Shared Library Manager / ASLM Examples / ExampleLibrary / Sources / TestExampleClass.cp < prev    next >
Encoding:
Text File  |  1996-11-19  |  5.5 KB  |  229 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        LibraryManagerTest.cp
  3.  
  4.     Contains:    MPW Tool to test out LibraryManager.
  5.  
  6.     Description:
  7.         
  8.         LibraryManagerTest1 [-v] [-t 0|1] [-c nReps]
  9.         
  10.         -v             turns verbose mode on (progress messages in MPW window), it is off by default
  11.         -t          turns tracing on (tracing to the Trace Monitor), it is off by default
  12.         -c nReps    nReps must be a positive integer for number of times through the test while loop
  13.         -s             shutdown the LibraryManager
  14.  
  15.         This test program can be modified to test any class you develop and do simple stress
  16.         testing by changing what is inside the test while loop.
  17.         
  18.     Caveats:
  19.     
  20.         Command-period will leave some things dangling that will never be disposed.
  21.         
  22.  
  23.     Copyright:    © 1991-1994 by Apple Computer, Inc., all rights reserved.
  24.  
  25. */
  26.  
  27.  
  28. #ifndef __EXAMPLECLASS__
  29. #include "ExampleClass.h"
  30. #endif
  31. #ifndef __LIBRARYMANAGERUTILITIES__
  32. #include <LibraryManagerUtilities.h>
  33. #endif
  34.  
  35. #ifndef __CTYPE__
  36. #include <ctype.h>
  37. #endif
  38. #ifndef __EVENTS__
  39. #include <Events.h>
  40. #endif
  41.  
  42. #include <MixedMode.h>
  43.  
  44.  
  45. extern "C" void DoTests(TExampleClass* object1, TExampleClass* object2, int count, Boolean trace, Boolean verbose, char* testClassInfoClass);
  46.  
  47. const kTestPoolSize = 0;    // set to 0 to test autogrow pools
  48. const ulong kTestHelloCount = 100000;
  49.  
  50. ulong helloticks = 0;
  51.  
  52. void testDynamicFunctions();
  53. char* localHello(ulong& theHelloTicks);
  54.  
  55. short sub[2] = { 0xD080, 0x4E75 };
  56.  
  57. main() {
  58.  
  59.     int paramNdx;
  60.     int theCount = 1;
  61.     Boolean verbose = true;
  62.     Boolean trace = true;
  63.     Boolean debugging = false;
  64.     char* testClassInfoClass = 0;
  65.     
  66.     DebugStr((unsigned char *)"\pTo get us into the Nub\n");
  67.     if (debugging) 
  68.         DebugStr((unsigned char *)"\p ExampleClassTest\n");
  69.  
  70.     // Get our LibraryManager
  71.     InitLibraryManager(kTestPoolSize, kSystemZone);
  72.     TLibraryManager* testMgr = GetLocalLibraryManager();
  73.     if (testMgr == NULL) {
  74.         return 1;
  75.     }
  76.  
  77.     if (LoadLibraries() != kNoError)
  78.     {
  79.         Trace("Bad News: Couldn't preload libraries\n");
  80.         CleanupLibraryManager();
  81.         return 1;
  82.     }
  83.     
  84.     // turn trace on or off according to -t option
  85.     if (!trace)
  86.         testMgr->TraceLogOff();
  87.     else
  88.         testMgr->TraceLogOn();
  89.         
  90.     testDynamicFunctions();
  91.     
  92.     // dump out LibraryManager info
  93.     //testMgr->Dump();
  94.     
  95.     // Create our test object
  96.     if (verbose) Trace("***Creating object1\n");
  97.     
  98.     SetSelfAsClient();
  99.  
  100.     // Create object with classId = kTExampleClassID
  101.     TExampleClass* object1 = (TExampleClass*) testMgr->NewObject(ClassID(kTExampleClassID));
  102.     if (!object1)
  103.         Trace("Could not create new object!!!\n");
  104.  
  105.     //testMgr->Dump();
  106.     
  107.     if (verbose) Trace("***Creating object2\n");
  108.  
  109.     // Create object with classId = kExampleClass
  110.     TExampleClass* object2 = (TExampleClass*) testMgr->NewObject(ClassID(kTExampleClassID));
  111.     if (!object2)
  112.         Trace("Could not create new object!!!\n");
  113.  
  114.     /* Call the code that will actually do the tests. */
  115.  
  116.     DoTests(object1, object2, theCount, trace, verbose, testClassInfoClass);
  117.     
  118.     if (verbose) Trace("***Disposing testMgr\n");
  119.     CleanupLibraryManager();    // delete the LibraryManager and its pool
  120.  
  121.     return 0;
  122. };
  123.  
  124.  
  125. void testDynamicFunctions()
  126. {
  127.     // Try out some dynamically linked functions
  128.     
  129.     // Now lets call Hello a bunch of times
  130.     ulong startTestHelloCount;
  131.     ulong helloCount = kTestHelloCount;
  132.     Hello(startTestHelloCount);
  133.     while (helloCount-- > 0)
  134.     {
  135.         ulong theTicks;
  136.         Hello(theTicks);
  137.     }
  138.     ulong endTestHelloCount;
  139.     Hello(endTestHelloCount);
  140.     Trace(" Iterations of Hello: \n");
  141.  
  142.     // Test the locally linked localHello() for a comparison
  143.     localHello(startTestHelloCount);
  144.     helloCount = kTestHelloCount;
  145.     while (helloCount-- > 0)
  146.     {
  147.         ulong theTicks;
  148.         localHello(theTicks);
  149.     }
  150.     localHello(endTestHelloCount);
  151. }
  152.  
  153. char* localHello(ulong& theHelloTicks)
  154. {
  155.     helloticks = TickCount();
  156.     theHelloTicks = helloticks;
  157.     return "Hello";
  158. }
  159.  
  160. extern "C" 
  161. void DoTests(TExampleClass* object1, TExampleClass* object2, int count, Boolean trace, Boolean /*verbose*/, char* testClassInfoClass)
  162. {
  163.     unsigned long endTicks, startTicks;
  164.     unsigned long saveCount = count;
  165.  
  166.     if (object1 && object2) 
  167.     {
  168.         startTicks = TickCount();
  169.         object1->SetObjectName("object1");
  170.         object2->SetObjectName("object2");
  171.         
  172.         while (count-- > 0)                 // <-- here is the test while loop
  173.         {                                    // we put calls to DoThisAndThat in here
  174.             object1->DoThisAndThat();        // here we test DoThisAndThat for object1
  175.             object2->DoThisAndThat();        // here we test DoThisAndThat for object2
  176.         }
  177.         
  178.         // test accessing globals
  179.         object1->SetGlobalInt(42);            // set the global using object1
  180.         
  181.         // get the global using object2
  182.         // since object1 and object2 share the same A5 world this will return
  183.         // 42!
  184.     
  185.         // Test the dynamically linked non-virtual member function GetGlobalRef
  186.         long* theGlobal;
  187.         object1->GetGlobalRef(theGlobal);
  188.         // Now theGlobal is the address of a global variable in the example library, 
  189.         // whose value should still be 42
  190.         
  191.         endTicks = TickCount();
  192.  
  193.         // Lets try TExampleClass::Test(char*), 
  194.         // note that this is a dynamically linked static member function
  195.         // it will return true since there are 2 instances of TExampleClass
  196.         Boolean hey = TExampleClass::Test("2");
  197.         Boolean hoo = TExampleClass::Test(2);
  198.         Boolean hee = TExampleClass::Test(3);
  199.     }
  200.  
  201.     if (object1) 
  202.     {
  203.         delete object1;
  204.     }
  205.  
  206.     if (object2) {
  207.         delete object2;
  208.     }
  209.     
  210.     // Now its time to really do something
  211.     if (testClassInfoClass)
  212.     {
  213.         TClassInfo* theInfo = GetLocalLibraryManager()->GetClassInfo(ClassID(testClassInfoClass));
  214.         if (theInfo)
  215.         {
  216.             do
  217.             {
  218.                 char    nameBuffer[256];    // for calls to GetVerboseName
  219.                 if (trace)
  220.                 {
  221.                     Trace("\nClass ID: %s\n", (char*) theInfo->GetClassID());
  222.                 }
  223.             } while (theInfo->Next());
  224.             
  225.             delete theInfo;
  226.         }
  227.     }
  228. }
  229.